package com.jsgt.greekfire.ui.wallet

import android.view.View
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import com.fhx.core.base.BaseActivity
import com.fhx.core.goToActivity
import com.jsgt.greekfire.App
import com.jsgt.greekfire.R
import com.jsgt.greekfire.ui.dialog.DialogUtils
import com.jsgt.greekfire.dao.model.IdentityBean
import com.jsgt.greekfire.dao.model.IdentityWallet
import com.jsgt.greekfire.dao.viewmodel.WalletViewModel
import com.jsgt.greekfire.ui.dialog.ExitDialog
import com.jsgt.greekfire.ui.me.backup.BackupActivity
import com.jsgt.greekfire.ui.wallet.export.ExportKeyActivity
import kotlinx.android.synthetic.main.activity_wallet_identity_manager.*

class WalletIdentityManagerActivity : BaseActivity(), View.OnClickListener {
    private val walletViewModel: WalletViewModel by lazy {
        ViewModelProvider(this).get(WalletViewModel::class.java)
    }
    private val exitDialog: ExitDialog by lazy {

        ExitDialog().okListener {
            identityBean?.let {
                DialogUtils.showInputPwdDialog(
                    this, identityBean!!.password
                ) {
                    showProgress(true)
                    walletViewModel.clearIdentity()
                    App.context.logout()
                }
            }
        }
    }
    private var wallet: IdentityWallet? = null

    private var identityBean: IdentityBean? = null

    override fun needFitsSystemWindows(): Boolean {
        return false
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_wallet_identity_manager
    }

    override fun main() {
        setStatusBar(R.color.transparent, false, R.color.white)

        title = getString(R.string.wallet_identity_management)
        wallet = intent.getParcelableExtra("wallet")
        if (wallet == null) {
            finish()
        }

        walletViewModel.getIdentity(wallet!!.identityId)?.observe(this, Observer {
            if (it != null) {
                identityBean = it
                txvName.text = it.identityName
                if (it.backup) {
                    txvExit.visibility = View.VISIBLE
                    txvBackupStatus.visibility = View.INVISIBLE
                } else {
                    txvExit.visibility = View.GONE
                    txvBackupStatus.visibility = View.VISIBLE
                }
            }
        })
        rlBackup.setOnClickListener(this)
        rlExport.setOnClickListener(this)
        rlAdd.setOnClickListener(this)
        rlInfo.setOnClickListener(this)
        txvExit.setOnClickListener(this)
    }

    override fun onClick(v: View?) {
        when (v?.id) {
            R.id.rlBackup -> {
                identityBean?.let {
                    DialogUtils.showInputPwdDialog(
                        this, identityBean!!.password
                    ) {
                        goToActivity<BackupActivity> {
                            putExtra("identityId", wallet!!.identityId)
                        }
                    }
                }
            }
            R.id.rlExport -> {
                identityBean?.let {
                    DialogUtils.showInputPwdDialog(
                        this, identityBean!!.password
                    ) {
                        goToActivity<ExportKeyActivity> {
                            putExtra("identityId", wallet!!.identityId)
                        }
                    }
                }
            }
            R.id.rlAdd -> {

            }
            R.id.rlInfo -> {
                if (identityBean != null) {
                    goToActivity<IdentityInfoActivity> {
                        putExtra("id", identityBean!!.id)
                    }
                }
            }
            R.id.txvExit -> {
//                logoutDialog.show()
                exitDialog.show(supportFragmentManager, "exit")
            }
        }
    }
}